﻿Shader "GPU/UI/GPUImage"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _Position("Position",vector) = (0,0,0,0)
        _Rotation("Rotation",vector) = (0,0,0,0)
        _Scale("Scale",vector) = (1,1,1,1)
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            // make fog work
            #pragma multi_compile_fog

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                UNITY_FOG_COORDS(1)
                float4 vertex : SV_POSITION;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;
            float4 _Position;
            float4 _Rotation;
            float4 _Scale;

            v2f vert (appdata v)
            {
                v2f o;
                //需要空间转换


                //平移
                float4x4 posMT = float4x4(
                    float4(1,0,0,_Position.x),
                    float4(0,1,0,_Position.y),
                    float4(0,0,1,_Position.z),
                    float4(0,0,0,1)
                );

                //旋转x
                //  float4x4 rotaX = float4x4(
                //     float4(1,0,0,0),
                //     float4(0,cos(_Rotation.x),0,0),
                //     float4(0,0,1,_Position.z),
                //     float4(0,0,0,1),
                // );

                //旋转y


                //旋转z
                 float4x4 rotaZ = float4x4(
                    float4(cos(_Rotation.z),-sin(_Rotation.z),0,0),
                    float4(sin(_Rotation.z),cos(_Rotation.z),0,0),
                    float4(0,0,1,0),
                    float4(0,0,0,1)
                );


                // 缩放矩阵
                float4x4 scaleMT = float4x4(
                    float4(_Scale.x,0,0,0),
                    float4(0,_Scale.y,0,0),
                    float4(0,0,_Scale.z,0),
                    float4(0,0,0,1)
                );
                v.vertex = mul(posMT,v.vertex);

                v.vertex = mul(rotaZ,v.vertex);

                v.vertex = mul(scaleMT,v.vertex);

               
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                UNITY_TRANSFER_FOG(o,o.vertex);
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                // sample the texture
                fixed4 col = tex2D(_MainTex, i.uv);
                // apply fog
                UNITY_APPLY_FOG(i.fogCoord, col);
                return col;
            }
            ENDCG
        }
    }
}
